<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="listproName">
        <el-input
          v-model="queryParams.listproName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态" prop="state">
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.state" clearable placeholder="请选择状态" @keyup.enter.native="handleQuery">
                <el-option
                  v-for="item in DropDownList"
                  :key="item.key"
                  :label="item.label"
                  :value="item.key"
                  
                ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ui:list_product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ui:list_product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ui:list_product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ui:list_product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list_productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="拼单商品id" align="center" prop="listproId" />
      <!-- <el-table-column label="店铺id" align="center" prop="shopId" /> -->
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <!-- <el-table-column label="分类id" align="center" prop="categoryId" /> -->
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <!-- <el-table-column label="1 ：拼单商品" align="center" prop="listproType" /> -->
      <el-table-column label="商品名称" align="center" prop="listproName" />
      <el-table-column label="原价" align="center" prop="listproPrice" />
      <!-- <el-table-column label="配送方式" align="center" prop="deliveryType" /> -->
      <el-table-column label="配送方式" align="center" prop="deliveryType">
      <template slot-scope="scope">
          <el-tag v-if="scope.row.deliveryType==1" type="success">商家配送</el-tag>
          <el-tag v-if="scope.row.deliveryType==2" type="warning">用户自提</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="现价" align="center" prop="listproReprice" />
      <el-table-column label="描述" align="center" prop="listproTitle" />
      <el-table-column label="库存" align="center" prop="listproStock" />
      <el-table-column label="总数" align="center" prop="listproNum" />
      <el-table-column label="商品图片" align="center" prop="listproImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.listproImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="state" /> -->
      <el-table-column label="状态" align="center" prop="state">
      <template slot-scope="scope">
          <el-tag v-if="scope.row.state==0" type="info">下架</el-tag>
          <el-tag v-if="scope.row.state==1" type="success">上架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ui:list_product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ui:list_product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改拼单商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="店铺id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺id" />
        </el-form-item> -->
        <el-form-item label="店铺" prop="shopName">
          <el-select v-model="form.shopId" clearable placeholder="请选择店铺" :style="{width: '100%'}">
                <el-option
                  v-for="item in ShopnameList"
                  :key="item.shopId"
                  :label="item.shopName"
                  :value="item.shopId"
                  
                ></el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="分类id" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类id" />
        </el-form-item> -->
        <el-form-item label="分类" prop="categoryName">
          <el-select v-model="form.categoryId" clearable placeholder="请选择分类" :style="{width: '100%'}">
                <el-option
                  v-for="item in ShopCategorynameList"
                  :key="item.categoryId"
                  :label="item.categoryName"
                  :value="item.categoryId"
                ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="1 ：拼单商品" prop="listproType">
          <el-input v-model="form.listproType" placeholder="请输入1 ：拼单商品" />
        </el-form-item> -->
        <el-form-item label="是否为拼单商品">
        <el-select v-model="form.listproType" clearable placeholder="默认为拼单商品">
                <el-option
                  v-for="item in listpro_typeList"
                  :key="item.key"
                  :label="item.label"
                  :value="item.key"
                ></el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="商品名称" prop="listproName">
          <el-input v-model="form.listproName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="原价" prop="listproPrice">
          <el-input v-model="form.listproPrice" placeholder="请输入原价" />
        </el-form-item>
        <!-- <el-form-item label="配送方式" prop="deliveryType">
          <el-input v-model="form.deliveryType" placeholder="请输入配送方式" />
        </el-form-item> -->
        <el-form-item label="配送方式">
        <el-select v-model="form.deliveryType" clearable placeholder="请选择配送方式">
                <el-option
                  v-for="item in deliveryList"
                  :key="item.key"
                  :label="item.label"
                  :value="item.key"
                ></el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="现价" prop="listproReprice">
          <el-input v-model="form.listproReprice" placeholder="请输入现价" />
        </el-form-item>
        <el-form-item label="描述" prop="listproTitle">
          <el-input v-model="form.listproTitle" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="库存" prop="listproStock">
          <el-input v-model="form.listproStock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="总数" prop="listproNum">
          <el-input v-model="form.listproNum" placeholder="请输入总数" />
        </el-form-item>
        <el-form-item label="商品图片">
          <image-upload v-model="form.listproImg"/>
        </el-form-item>
        <!-- <el-form-item label="状态" prop="state" >
          <el-select v-model="form.state"  clearable :style="{width: '100%'}" placeholder="请选择">
            <el-option label="下架" value="0" ></el-option>
            <el-option label="上架" value="1"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态">
        <el-select v-model="form.state" clearable placeholder="请选择状态">
                <el-option
                  v-for="item in DropDownList"
                  :key="item.key"
                  :label="item.label"
                  :value="item.key"
                ></el-option>
        </el-select>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listList_product, getList_product, delList_product, addList_product, updateList_product,listshop_name,listshop_gategoryname } from "@/api/ui/list_product";

export default {
  name: "List_product",
  data() {
    return {
      ShopnameList:[],
      ShopCategorynameList:[],
      
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼单商品管理表格数据
      list_productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        listproName: null,
        state: null,
        deliveryType:null,
      },
      // 表单参数
      form: {},
      DropDownList:[
          {key: 0,label: "下架"},
          {key: 1,label: "上架"},
      ],
      deliveryList:[
        {key: 1, label: "商家配送"},
        {key: 2, label: "用户自提"},
      ],
      listpro_typeList:[
        {key: 0, label: "普通商品"},
        {key: 1, label: "拼单商品"}
      ],
      // 表单校验
      rules: {
        shopId: [
          { required: true, message: "店铺id不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "分类id不能为空", trigger: "blur" }
        ],
        listproType: [
          { required: true, message: "1 ：拼单商品不能为空", trigger: "blur" }
        ],
        listproName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        listproPrice: [
          { required: true, message: "原价不能为空", trigger: "blur" }
        ],
        deliveryType: [
          { required: true, message: "配送方式 1:商家配送 2：用户自提不能为空", trigger: "blur" }
        ],
        listproReprice: [
          { required: true, message: "现价不能为空", trigger: "blur" }
        ],
        listproTitle: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        listproStock: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        listproNum: [
          { required: true, message: "总数不能为空", trigger: "blur" }
        ],
        listproImg: [
          { required: true, message: "商品图片不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态 0下架 1上架  默认上架不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getShopnameList();
    this.getShopCategorynameList();
  },
  methods: {
    /** 查询拼单商品管理列表 */
    getList() {
      this.loading = true;
      listList_product(this.queryParams).then(response => {
        this.list_productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     * 下拉框
     */
     getShopnameList(){
      this.loading = true;
      listshop_name(this.queryParams).then(response =>{
        console.log(response)
        this.ShopnameList=response.data
        console.log(this.ShopnameList)
        this.loading = false;
      });
    },
    getShopCategorynameList(){
      this.loading=true;
      listshop_gategoryname(this.queryParams).then(response =>{
        this.ShopCategorynameList=response.data
        this.loading=false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        listproId: null,
        shopId: null,
        categoryId: null,
        listproType: null,
        listproName: null,
        listproPrice: null,
        deliveryType: null,
        listproReprice: null,
        listproTitle: null,
        listproStock: null,
        listproNum: null,
        listproImg: null,
        state: null,
        deliveryType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.listproId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼单商品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const listproId = row.listproId || this.ids
      getList_product(listproId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拼单商品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.listproId != null) {
            updateList_product(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addList_product(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const listproIds = row.listproId || this.ids;
      this.$modal.confirm('是否确认删除拼单商品管理编号为"' + listproIds + '"的数据项？').then(function() {
        return delList_product(listproIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ui/list_product/export', {
        ...this.queryParams
      }, `list_product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
